package com.cet.spring_batch_study.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.GZIPOutputStream;

/**
 * @author cet
 */
public class FileUtil {

    private FileUtil() {

    }

    public static void compressFile(String inputFile, String outputFile) throws IOException {
        try (InputStream in = Files.newInputStream(Paths.get(inputFile));
             OutputStream out = new FileOutputStream(outputFile)) {

            GZIPOutputStream gzipOut = new GZIPOutputStream(out);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer))!= -1) {
                gzipOut.write(buffer, 0, length);
            }
            gzipOut.finish();

        } catch (IOException e) {
            throw new IOException("Error compressing file", e);
        }
    }
}
